package com.stock.stockana.requests;

import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.stock.stockana.constants.NetWorkConstants;
import com.stock.stockana.events.network.GetOneStockEvent;

import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;


/**
 * ---------------------------------------------------
 * Description: 根据股票号码获取指定stock的请求类
 * Author: ljgsonx
 * Belong to: com.wds.stock.stockana.requests
 * Time: 2016/1/15 17:42
 * ---------------------------------------------------
 */
public class GetOneStock extends BaseStringRequest<GetOneStock> {

    private final static String TAG = "GetOneStock--->";

    private String mStockNum;

    public final static int ACTION_ADDSTOCK = 100;
    public final static int ACTION_OPENSTOCK = 101;
    public final static int ACTION_UPDATEPRICE = 102;
    public final static int ACTION_OPENSTOCK_INPUSHRECORD = 103;

    private static int action = ACTION_ADDSTOCK;

    private GetOneStock(){
        super(Method.POST, NetWorkConstants.GET_ONESTOCK_URL, listener, errorListener);
        Log.d(TAG, NetWorkConstants.GET_ONESTOCK_URL);
    }

    static Response.Listener<String> listener = new Response.Listener<String>(){

        @Override
        public void onResponse(String response) {
            if("{".charAt(0) == response.charAt(0)) {
                GetOneStockEvent gose = new GetOneStockEvent(NetWorkConstants.RESPONSE_CODE_OK, null);
                gose.setmStockInfoBeanStr(response);
                gose.setAction(action);
                EventBus.getDefault().post(gose);
            }else{
                EventBus.getDefault().post(new GetOneStockEvent(NetWorkConstants.RESPONSE_CODE_ERROR,NetWorkConstants.RESPONSE_ERROR1 + response));
            }
        }
    };

    static Response.ErrorListener errorListener = new Response.ErrorListener(){

        @Override
        public void onErrorResponse(VolleyError error) {
            EventBus.getDefault().post(new GetOneStockEvent(NetWorkConstants.RESPONSE_CODE_ERROR, NetWorkConstants.RESPONSE_ERROR3 + error.toString()));
        }
    };

    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        Map<String, String> map = new HashMap<>();
        map.put("stockNum", mStockNum);
        return map;
    }

    @Override
    public Request<?> setTag(Object tag) {
        return super.setTag(TAG);
    }

    public static String getTagStr(){
        return TAG;
    }

    public static GetOneStock build() {
        return new GetOneStock();
    }

    public GetOneStock setStockNum(String stockNum) {
        this.mStockNum = stockNum;
        return this;
    }

    public GetOneStock setAction(int action){
        GetOneStock.action = action;
        return this;
    }

}
